clear all;

cd ..; cd ..; cd ..; cd ..; 
load('code_main_model\6_implied_w_r\computation_results\data.mat');
cd 'code_figure_table\maintext\Table 2\model';

r_end_index=min(find(r_iterate_ini==0))-1;
w_end_index=min(find(w_iterate_ini(r_end_index,:,1)==0))-1;

% interest rate and farmer
r=r_iterate_ini(r_end_index);
frac_farmer_1986=sum(labor_supply_iterate_ini(r_end_index,w_end_index,:)-labor_demand_iterate_ini(r_end_index,w_end_index,:));

% migration rate and ratio
mig=sum(mig_trans,[3,4,5,6]);
total_mig(1:11)=nan;
bank_mig(1:11)=nan;
unbank_mig(1:11)=nan;
for t=1:11
    total_mig(t)=sum(mig(t,:));
    bank_index=find(d(t,:)==0);
    unbank_index=find(d(t,:)~=0);
    bank_mig(t)=sum(mig(t,bank_index));
    unbank_mig(t)=sum(mig(t,unbank_index));
end
ratio=bank_mig./unbank_mig;

mig_86to96=sum(total_mig);
out_mig_ratio=mean(ratio,'omitnan');

% deposit
deposit_allmkt=sum(deposit(1:11,:),2);
cash_allmkt=sum(cash(1:11,:),2);
deposit_cash_ratio=mean(deposit_allmkt./cash_allmkt);

deposit_cash_ratio_mkt=deposit(1:11,:)./cash(1:11,:);
d=d(1:11,:);
ctrl_Z(1:11,1:nmkt)=0;
for i=1:nmkt
    ctrl_Z(:,i)=Z(i);
end
mdl = fitlm([d(:),ctrl_Z(:)],deposit_cash_ratio_mkt(:));
deposit_sensitivity=table2array(mdl.Coefficients(2,1));

% loan
frac_loan=sum(frac_credit(1,:));

save('basic_stats.mat','r','frac_farmer_1986','mig_86to96','out_mig_ratio','deposit_cash_ratio','deposit_sensitivity','frac_loan')